#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
char a[100] = {0};
sem_t sem, sem2;

void *handler_fun(void *arg) {
    while(1) {
        sem_wait(&sem);//有资源可以是同，没有资源就阻塞等待
        if(strcmp(a, "exit") == 0)
            break;
        printf("buf:%s\n", a);  
        sem_post(&sem2);//释放资源 
    }

}
int main() {
    pthread_t tid;
    if(sem_init(&sem2, 0, 1)<0){
        perror("sem2 init err\n");
        return -1;
    }
    if(sem_init(&sem, 0, 0)<0) {
        perror("sem init err\n");
        return -1;
    }
    if(pthread_create(&tid, NULL, handler_fun, &a) != 0) {
        perror("pthread create err\n");
        return -1;
    }
    printf("in main\n");
    do{
        sem_wait(&sem2);
        gets(a);
        sem_post(&sem);
    if(strcmp(a, "exit") == 0)
        break;
    }while (1);
    pthread_join(tid, NULL);
    return 0;
}